package com.wande.dataplatform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wande.common.core.utils.MapstructUtils;
import com.wande.common.core.utils.StringUtils;
import com.wande.common.mybatis.core.page.PageQuery;
import com.wande.common.mybatis.core.page.TableDataInfo;
import com.wande.dataplatform.domain.CheckRule;
import com.wande.dataplatform.domain.bo.CheckRuleBo;
import com.wande.dataplatform.domain.vo.CheckRuleVo;
import com.wande.dataplatform.mapper.CheckRuleMapper;
import com.wande.dataplatform.service.ICheckRuleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 数据稽查规则服务实现
 *
 * @author wande
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CheckRuleServiceImpl implements ICheckRuleService {

    private final CheckRuleMapper checkRuleMapper;

    @Override
    public TableDataInfo<CheckRuleVo> queryPageList(CheckRuleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CheckRule> wrapper = buildQueryWrapper(bo);
        Page<CheckRule> page = checkRuleMapper.selectPage(pageQuery.build(), wrapper);
        Page<CheckRuleVo> voPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        voPage.setRecords(BeanUtil.copyToList(page.getRecords(), CheckRuleVo.class));
        return TableDataInfo.build(voPage);
    }

    @Override
    public List<CheckRuleVo> queryList(CheckRuleBo bo) {
        LambdaQueryWrapper<CheckRule> wrapper = buildQueryWrapper(bo);
        List<CheckRule> list = checkRuleMapper.selectList(wrapper);
        return BeanUtil.copyToList(list, CheckRuleVo.class);
    }

    @Override
    public CheckRuleVo queryById(Long id) {
        CheckRule entity = checkRuleMapper.selectById(id);
        return BeanUtil.toBean(entity, CheckRuleVo.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insertByBo(CheckRuleBo bo) {
        CheckRule entity = BeanUtil.toBean(bo, CheckRule.class);
        checkRuleMapper.insert(entity);
        return entity.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(CheckRuleBo bo) {
        CheckRule entity = BeanUtil.toBean(bo, CheckRule.class);
        return checkRuleMapper.updateById(entity) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteByIds(List<Long> ids) {
        return checkRuleMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<CheckRuleVo> queryByDimension(String dimension) {
        LambdaQueryWrapper<CheckRule> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CheckRule::getDimension, dimension);
        wrapper.eq(CheckRule::getStatus, 1);
        List<CheckRule> list = checkRuleMapper.selectList(wrapper);
        return BeanUtil.copyToList(list, CheckRuleVo.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean importRules(List<CheckRuleBo> rules) {
        for (CheckRuleBo bo : rules) {
            insertByBo(bo);
        }
        return true;
    }

    private LambdaQueryWrapper<CheckRule> buildQueryWrapper(CheckRuleBo bo) {
        LambdaQueryWrapper<CheckRule> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotBlank(bo.getName()), CheckRule::getName, bo.getName());
        wrapper.eq(StringUtils.isNotBlank(bo.getCode()), CheckRule::getCode, bo.getCode());
        wrapper.eq(StringUtils.isNotBlank(bo.getDimension()), CheckRule::getDimension, bo.getDimension());
        wrapper.eq(StringUtils.isNotBlank(bo.getRuleType()), CheckRule::getRuleType, bo.getRuleType());
        wrapper.eq(bo.getStatus() != null, CheckRule::getStatus, bo.getStatus());
        wrapper.eq(bo.getProjectId() != null, CheckRule::getProjectId, bo.getProjectId());
        wrapper.orderByDesc(CheckRule::getCreateTime);
        return wrapper;
    }
}
